import CryptoJS from 'crypto-js/crypto-js'

/**
 * AES加密 ：字符串 key iv  返回base64 
 */
var keyString = "xjgw20190828success"

export function Encrypt(word) {
    let userId = JSON.parse(localStorage.getItem('userInfor')).userId
    let keyStr = (userId + keyString).substring(0, 16)
    console.log(keyStr)
    var key = CryptoJS.enc.Utf8.parse(keyStr)
    var srcs = CryptoJS.enc.Utf8.parse(word)
    var encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }) // 加密模式为ECB，补码方式为PKCS5Padding（也就是PKCS7）
    return encrypted.toString()

}
/**
 * AES 解密 ：字符串 key iv  返回base64 
 *
 */
export function Decrypt(word) {
    let userId = JSON.parse(localStorage.getItem('userInfor')).userId
    let keyStr = (userId + keyString).substring(0, 16)
    console.log(keyStr)
    var key = CryptoJS.enc.Utf8.parse(keyStr)
    var decrypt = CryptoJS.AES.decrypt(word, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
    return CryptoJS.enc.Utf8.stringify(decrypt).toString()
}